<template>
	<!-- 当有值则为true  没有值或者total为0 则为false 就隐藏了 -->
	<div class="todo-footer" v-show="total">
		<label>
			<!-- <input type="checkbox" :checked="isAll" @change="checkAll"/> -->
			<!-- checkbox  的值 是 ture  or  false -->
			<input type="checkbox" v-model="isAll"/>
		</label>
		<span>
			<span>已完成{{doneTotal}}</span> / 全部{{total}}
		</span>
		<button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
	</div>
</template>

<script>
    export default {
        name:'MyFooter',
        props:['todos','checkAllTodo','clearAllTodo'],
        computed: {
            total() {
                return this.todos.length
            },
			//已经完成的总数
            doneTotal() {
				//使用reduce进行统计    pre  ，todo 当前项    todo.done如果勾选返回1 没勾选返回0
                return this.todos.reduce((pre,todo) => pre + (todo.done ? 1 : 0) ,0)
            },
			// doneTotal() {
			// 	let i = 0;
			// 	this.todos.forEach(todo => {
			// 		if(todo.done) i++;
			// 	});
			// 	return i;
			// },
             isAll: {
                get() {
					//如果勾选的和全部是相同的 就全选    this.total>0 保持总数大于0并且 全部相等才是全部勾选
                    return this.doneTotal === this.total && this.total > 0 
                },
                set(value) {
					//设置修改的值为 value ture  or  false
                    this.checkAllTodo(value) 
                }
            },
        },
       
        methods: {
            clearAll() {
                this.clearAllTodo()
            }
        }
        
    }
</script>


<style scoped>
	/*footer*/
	.todo-footer {
		height: 40px;
		line-height: 40px;
		padding-left: 6px;
		margin-top: 5px;
	}

	.todo-footer label {
		display: inline-block;
		margin-right: 20px;
		cursor: pointer;
	}

	.todo-footer label input {
		position: relative;
		top: -1px;
		vertical-align: middle;
		margin-right: 5px;
	}

	.todo-footer button {
		float: right;
		margin-top: 5px;
	}
</style>